package controller.thanhvien;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Hoadon;
import pojo.Sanpham;
import pojo.Taikhoan;
import dao.HoadonDAO;
import dao.TaikhoanDAO;

/**
 * Servlet implementation class Header
 */
@WebServlet("/Pages/TrangTaiKhoanThanhVien.do")
public class TrangTaiKhoanThanhVien extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TrangTaiKhoanThanhVien() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");

		HttpSession session = request.getSession();

		String action = request.getParameter("action");
		String url = "";
		
		if (action.equals("TrangTaiKhoanThanhVien")) {

			request.setAttribute("page", "TrangTaiKhoanThanhVien");
			url = "../index.jsp";
		}
		
		if (action.equals("LietKeToanBo")) {

			Taikhoan tk = (Taikhoan)session.getAttribute("taiKhoan");
			
			HoadonDAO hddao = new HoadonDAO();
			List<Hoadon> lstHoaDon = hddao.GetHoaDon(tk.getMataikhoan());

			// Lấy tài khoản đang đăng nhập hiện tại
			// Ép các hóa đơn "tương ứng tài khoản trên"
			// tự tính tông tiền (vì chưa có cột tổng tiền)	
			for (int i = 0; i < lstHoaDon.size(); i++)
				lstHoaDon.get(i).TinhTongTien();

			request.setAttribute("listHoaDon", lstHoaDon);
			request.setAttribute("page", "TrangTaiKhoanThanhVien");
			url = "../index.jsp";
		}
		
		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}
}
